function initPage() {
	changeMenu("general");

	for (var i = 0; i < reportStructure.tabs.length; i++) {
		var tab = reportStructure.tabs[i];

		for (var j = 0; j < tab.categories.length; j++) {
			hideCategory(tab.categories[j]);
		}
		
		var firstCategory = tab.firstCategory;		
		activateCategory(tab, firstCategory);
	}

	for (var i = 0; i < reportStructure.usageTabs.length; i++) {
		var tab = reportStructure.usageTabs[i];
		linkUsageTab(tab);

		hideAllUsagePages(tab);
		
		var firstCategory = tab.firstCategory;
		var firstPage = firstCategory.firstPage;
		
		var pageIdentifier = firstCategory.categoryId + " " + firstPage.pageId;
		selectUsagePage(tab.tabId, pageIdentifier);
	}
}

function createSubcategory(subcategoryId, divId) {
	var subcategory = new Object();
	subcategory.subcategoryId = subcategoryId;
	subcategory.divId = divId;
	
	return subcategory;
}

function createCategory(categoryId, subcategories, firstSubcategory, selectId) {
	var category = new Object();
	
	category.categoryId = categoryId;
	category.subcategories = subcategories;
	category.firstSubcategory = firstSubcategory;
	category.lastSubcategory = firstSubcategory;
	category.selectId = selectId;
	
	return category;
}

function createTab(tabId, categories, firstCategory, selectId) {
	var tab = new Object();
	
	tab.tabId = tabId;
	tab.categories = categories;
	tab.firstCategory = firstCategory;
	tab.selectId = selectId;

	return tab;
}

function hideAllElements(elementsIds) {
	for (var i = 0; i < elementsIds.length; i++) {
		document.getElementById(elementsIds[i]).style.display = "none";
	}
}

function activateRootCauseCategory(selectedId, selectsIds) {
	
	hideAllElements(selectsIds);
	document.getElementById(selectedId).style.display = "inline-block";	
}

function getSelectedTab(selectionIdentifier) {
	var tabId = selectionIdentifier.tabId;

	for (var i = 0; i < reportStructure.tabs.length; i++) {
		var tab = reportStructure.tabs[i];
		if (tab.tabId == tabId) {
			return tab;
		}
	}
	
	return null;
}

function getSelectedCategory(selectionIdentifier) {
	var tab = getSelectedTab(selectionIdentifier);	
	var categoryId = selectionIdentifier.categoryId;
	
	for (var i = 0; i < tab.categories.length; i++) {
		var category = tab.categories[i];
		if (category.categoryId == categoryId) {
			return category;
		}
	}
		
	return null;
}

function getSelectedSubcategory(selectionIdentifier) {
	var category = getSelectedCategory(selectionIdentifier);
	var subcategoryId = selectionIdentifier.subcategoryId;
	
	for (var i = 0; i < category.subcategories.length; i++) {
		var subcategory = category.subcategories[i];
		if (subcategory.subcategoryId == subcategoryId) {
			return subcategory;
		}
	}
		
	return null;
}

function selectSubcategory(selectionIdentifier, subcategoryId) {
	selectionIdentifier.subcategoryId = subcategoryId;
	
	var selectedCategory = getSelectedCategory(selectionIdentifier);
	var selectedSubcategory = getSelectedSubcategory(selectionIdentifier);

	for (var i = 0; i < selectedCategory.subcategories.length; i++) {
		var subcategory = selectedCategory.subcategories[i];
		if (selectedSubcategory.subcategoryId == subcategory.subcategoryId) {
			continue;
		}

		hideSubcategory(subcategory);
	}

	activateSubcategory(selectedCategory, selectedSubcategory);
}

function selectCategory (selectionIdentifier, categoryId) {
	selectionIdentifier.categoryId = categoryId;
	
	var selectedTab = getSelectedTab(selectionIdentifier);
	var selectedCategory = getSelectedCategory(selectionIdentifier);

	for (var i = 0; i < selectedTab.categories.length; i++) {
		var category = selectedTab.categories[i];
		if (selectedCategory.categoryId == category.categoryId) {
			continue;
		}
		
		hideCategory(category);
	}
	
	activateCategory(selectedTab, selectedCategory);	
	selectSelect(selectedTab.selectId, selectedCategory.categoryId);	
}

function hideSubcategory(subcategory) {
	var subcategoryDivId = subcategory.divId;
	document.getElementById(subcategoryDivId).style.display = "none";	
}

function hideCategory(category) {
	var selectId = category.selectId;
	document.getElementById(selectId).style.display = "none";
	
	for (var i = 0; i < category.subcategories.length; i++) {
		hideSubcategory(category.subcategories[i]);
	}
}

function activateSubcategory(category, subcategory) {
	category.lastSubcategory = subcategory;
	
	var divId = subcategory.divId;
	document.getElementById(divId).style.display = "block";
	
	selectSelect(category.selectId, subcategory.subcategoryId);	
}

function activateCategory(tab, category) {
	var selectId = category.selectId;
	document.getElementById(selectId).style.display = "inline-block";
	
	activateSubcategory(category, category.lastSubcategory);
	
	selectSelect(tab.selectId, category.categoryId);
}

function selectSelect(selectId, optionValue) {
	var selectElement = document.getElementById(selectId);
	if (selectElement == null) {
		// select does not have exist
		return;
	}
	
	var approptiateOption = null;
	for (var i = 0; i < selectElement.options.length; i++) {
		var option = selectElement.options[i];
		
		if (option.value == optionValue) {
			approptiateOption = option;
			break;
		}
	}
	
	if (approptiateOption == null) {
		selectElement.selectedIndex = -1;
	} else {
		selectElement.selectedIndex = approptiateOption.index;
	}
}

// Uncomment for use of 2011-2012 study
//var menuElements = ["general", "detection", "root_cause", "used_methods", "used_tools", "optional_menu"];
//var tabElements = ["generalTab", "detectionTab", "rootCauseTab", "usedMethodsTab", "toolsTab", "optionalTab"];

var menuElements = ["general", "used_tools", "optional_menu"];
var tabElements = ["generalTab", "toolsTab", "optionalTab"];

function changeMenu(clickedId) {
		
	var clickedIndex = 0;
	var shownTabId = "";
	
	for (var i = 0; i < menuElements.length; i++) {
		if (menuElements[i] == clickedId) {
			shownTabId = tabElements[i];
		}
		
		var menuElementId = menuElements[i];
		var element = document.getElementById(menuElementId); 
		element.className = "";
	}
	
	hideAllElements(tabElements);
	
	document.getElementById(clickedId).className = "active";
	document.getElementById(shownTabId).style.display = "block";
}

function moveMenu(next) {
	
	var activeIndex = -1;
	for (i in menuElements) {
		if (document.getElementById(menuElements[i]).className == "active") {
			activeIndex = i;
			break;
		}
	}

	activeIndex = parseInt(activeIndex);
	if (activeIndex == -1) {
		return;
	}
	
	if (next) {
		activeIndex = (activeIndex == (menuElements.length - 1)) ? 0 : (activeIndex + 1); 		
	} else {
		activeIndex = (activeIndex == 0) ? (menuElements.length - 1) : (activeIndex - 1);		
	}
	
	changeMenu(menuElements[activeIndex]);
}

function createUsageItem(itemId, rowId) {
	var item = new Object();
	item.itemId = itemId;
	item.rowId = rowId;
	
	return item;
}

function createUsagePage(pageId, items, categoryId) {
	var page = new Object();
	page.pageId = pageId;
	page.items = items;
	page.nextPage = null;
	page.previousPage = null;
	page.categoryId = categoryId;
	
	return page;
}

function createUsageCategory(categoryId, pages, firstPage) {
	var category = new Object();
	
	category.categoryId = categoryId;
	category.pages = pages;
	category.firstPage = firstPage;
	
	return category;
}

function createUsageTab(tabId, categories, firstCategory, selectId) {
	var tab = new Object();
	
	tab.tabId = tabId;
	tab.categories = categories;
	tab.firstCategory = firstCategory;
	tab.selectId = selectId;
	tab.currentPage = firstCategory.firstPage;
	
	return tab;
}

function getUsageTab(tabId) {
	for (var i = 0; i < reportStructure.usageTabs.length; i++) {
		var tab = reportStructure.usageTabs[i];
		if (tab.tabId == tabId) {
			return tab;
		}
	}
		
	return null;	
}

function getUsageCategory(tabId, categoryId) {
	var tab = getUsageTab(tabId);
	
	for (var i = 0; i < tab.categories.length; i++) {
		var category = tab.categories[i];
		if (category.categoryId == categoryId) {
			return category;
		}
	}
		
	return null;	
}

function getUsagePage(tabId, categoryId, pageId) {
	var category = getUsageCategory(tabId, categoryId);
	
	for (var i = 0; i < category.pages.length; i++) {
		var page = category.pages[i];
		if (page.pageId == pageId) {
			return page;
		}
	}
		
	return null;	
}

function getUsageItem(tabId, categoryId, pageId, itemIde) {
	var pageId = getUsagePage(tabId, categoryId, pageId);
	
	for (var i = 0; i < page.items.length; i++) {
		var item = page.items[i];
		if (item.itemId == itemId) {
			return item;
		}
	}
		
	return null;
}

function changeUsageVisibilityPage(page, visible) {
	
	var displayStyle = visible ? "table-row" : "none";
	for (var i = 0; i < page.items.length; i++) {
		var item = page.items[i];
		var rowId = item.rowId;
		
		document.getElementById(rowId).style.display = displayStyle;
	}	
}

function hideAllUsagePages(tab) {
	for (var i = 0; i < tab.categories.length; i++) {
		var category = tab.categories[i];
		
		for (var j = 0; j < category.pages.length; j++) {
			var page = category.pages[j];
			changeUsageVisibilityPage(page, false);
		}
	}
}

function selectUsagePage(tabId, pageIdentifierString) {

	var splitted = pageIdentifierString.split(" ");
	
	var categoryId = splitted[0];
	var pageId = splitted[1];
	
	var page = getUsagePage(tabId, categoryId, pageId);
	var tab = getUsageTab(tabId);

	hideAllUsagePages(tab);
	changeUsageVisibilityPage(page, true);
	
	selectSelect(tab.selectId, pageIdentifierString);
	tab.currentPage = page;
}

function linkUsageTab(tab) {
	var previousPage = null;
	var firstPage = null;
	
	for (var i = 0; i < tab.categories.length; i++) {
		var category = tab.categories[i];
		
		for (var j = 0; j < category.pages.length; j++) {
			var page = category.pages[j];

			if ((i == 0) && (j == 0)) {
				firstPage = page; 
			}
			
			if (previousPage != null) {
				page.previousPage = previousPage; 
			}
			
			previousPage = page;
		}
	}
	
	if (firstPage != null) {
		firstPage.previousPage = previousPage;
	}
	
	var nextPage = null;
	var lastPage = null;
	for (var i = 0; i < tab.categories.length; i++) {
		var backwardI = (tab.categories.length - 1) - i;
		var category = tab.categories[backwardI];
		
		for (var j = 0; j < category.pages.length; j++) {
			var backwardJ = (category.pages.length - 1) - j;
			var page = category.pages[backwardJ];

			if ((i == 0) && (j == 0)) {
				lastPage = page; 
			}
			
			if (nextPage != null) {
				page.nextPage = nextPage; 
			}
			
			nextPage = page;
		}
	}	
	
	if (lastPage != null) {
		lastPage.nextPage = nextPage;
	}
}

function moveTableCategory(tabId, forward) {
	var tab = getUsageTab(tabId);
	var currentPage = tab.currentPage;
	
	var newPage = forward ? currentPage.nextPage : currentPage.previousPage;
	selectUsagePage(tabId, newPage.categoryId + " " + newPage.pageId);
}
